package com.huifer.wgx.service.impl;

import com.huifer.wgx.model.OperationEntity;
import com.huifer.wgx.repository.OperationRepository;
import com.huifer.wgx.service.OperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

/**
 * <p>Title : OperationServiceImpl </p>
 * <p>Description : </p>
 *
 * @author huifer
 * @date 2019-03-19
 */
@Service
public class OperationServiceImpl implements OperationService {

    @Autowired
    private OperationRepository operationRepository;


    /**
     * 保存操作
     *
     * @param note 操作内容
     * @param perssonelId 操作人id
     * @return 操作
     */
    @Override
    public OperationEntity saveOperation(String note, Long perssonelId) {
        try {
            OperationEntity op = new OperationEntity();
            op.setOperationName(note);
            op.setOperationId(perssonelId);

            return operationRepository.save(op);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 查询所有操作
     *
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return 操作列表
     */
    @Override
    public Page<OperationEntity> findAll(Integer pageNum, Integer pageSize) {
        Sort sort = new Sort(Direction.DESC, "operationId");
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, sort);
        Page<OperationEntity> all = operationRepository.findAll(pageable);
        return all;
    }
}
